package com.stevenmoon.csdnplus.activity;

import android.content.res.Configuration;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;

import com.stevenmoon.csdnplus.R;
import com.stevenmoon.csdnplus.activity.base.BaseActivity;
import com.stevenmoon.csdnplus.event.DrawerItemClickEvent;
import com.stevenmoon.csdnplus.fragment.HeadlineMainFragment;
import com.stevenmoon.csdnplus.fragment.MainMenuFragment;
import com.stevenmoon.csdnplus.util.StatusBarUtil;

import org.simple.eventbus.Subscriber;

import butterknife.Bind;

public class MainActivity extends BaseActivity {

    //@Nullable
    @Bind(R.id.drawerLayout)
    DrawerLayout mDrawerLayout;

    ActionBarDrawerToggle mDrawerToggle;

    Fragment mHeadlineFrag;
    Fragment mBlogFrag;
    Fragment mAskFrag;
    Fragment mForumFrag;
    Fragment mProfileFrag;

    @Override
    protected void initViews() {
        //TODO 无法实现透明状态栏，toolbar会被遮盖住
        //StatusBarUtil.setTranslucentForDrawerLayout(MainActivity.this, mDrawerLayout, 127);
        StatusBarUtil.setTranslucentForDrawerLayout(this,mDrawerLayout);
        //这个类可以方便的将 DrawerLayout，appbar绑定在一起，自动添加一个汉堡按钮，并且打开关闭时带有动画显示
        mDrawerToggle= new ActionBarDrawerToggle(
                this, mDrawerLayout, R.string.app_name, R.string.app_name);
        //设置监听器为toggle
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        //启动绑定！
        mDrawerToggle.syncState();
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.menuContainer, MainMenuFragment.newInstance())
                .commit();
        //TODO 根据配置或者上一次关闭的位置打开
        replaceFragment(0);
    }

    public void toggleMenu(){
        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        } else {
            mDrawerLayout.openDrawer(GravityCompat.START);
        }
    }

    @Subscriber
    void OnDrawerItemClick(DrawerItemClickEvent event){
        int selectedIndex = event.getSelectedIndex();
        replaceFragment(selectedIndex);
        toggleMenu();

    }

    private void replaceFragment(int selectedIndex){
        //TODO 这样写肯定不合适，需要重构
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (mHeadlineFrag != null) {
            ft.hide(mHeadlineFrag);
        }
        if (mBlogFrag != null) {
            ft.hide(mBlogFrag);
        }
        if (mAskFrag != null) {
            ft.hide(mAskFrag);
        }
        if (mForumFrag != null) {
            ft.hide(mForumFrag);
        }
        if (mProfileFrag != null) {
            ft.hide(mProfileFrag);
        }
        switch (selectedIndex){
            case 0:
                if(mHeadlineFrag==null){
                    mHeadlineFrag = new HeadlineMainFragment();
                    ft.add(R.id.contentContainer,mHeadlineFrag);
                }else{
                    ft.show(mHeadlineFrag);
                }
                break;
            case 1:
                if(mBlogFrag==null){
                    mBlogFrag = new HeadlineMainFragment();
                    ft.add(R.id.contentContainer,mBlogFrag);
                }else{
                    ft.show(mBlogFrag);
                }
                break;
            case 2:
                if(mAskFrag==null){
                    mAskFrag = new HeadlineMainFragment();
                    ft.add(R.id.contentContainer,mAskFrag);
                }else{
                    ft.show(mAskFrag);
                }
                break;
            case 3:
                if(mForumFrag==null){
                    mForumFrag = new HeadlineMainFragment();
                    ft.add(R.id.contentContainer,mForumFrag);
                }else{
                    ft.show(mForumFrag);
                }
                break;
            case 4:
                if(mProfileFrag==null){
                    mProfileFrag = new HeadlineMainFragment();
                    ft.add(R.id.contentContainer,mProfileFrag);
                }else{
                    ft.show(mProfileFrag);
                }
                break;
        }
        ft.commit();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    public void onBackPressed() {
        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
}
